import java.util.Scanner;
import java.util.Random;

public class Main {

    /**
     判断年份是否为闰年
     */
    public static void leapYear() {
        Scanner scanner = new Scanner(System.in);

        System.out.print("输入年份：");

        int year = scanner.nextInt();

        if((year % 400==0) || (year%4==0 && year%100!=0)){
            System.out.println(year+"是闰年");
        }else{
            System.out.println(year+"不是闰年");
        }

        scanner.close();
    }

    /**
     判断成绩是`优秀`、`良好`、`中等`、`及格`、`不及格`
     */
    public static void grade() {
        Scanner scanner = new Scanner(System.in);

        System.out.print("输入分数：");

        int score = scanner.nextInt();

        if(score>=90){
            System.out.println("优秀");
        } else if (score>=80) {
            System.out.println("良好");
        } else if (score>=70) {
            System.out.println("中等");
        } else if (score>=60) {
            System.out.println("及格");
        }else{
            System.out.println("不及格");
        }

        scanner.close();
    }

    /**
     摄氏度、华氏度的转换
     */
    public static void convCF() {
        Scanner scanner = new Scanner(System.in);

        System.out.print("输入0是摄氏转华氏度，输入1是华氏转摄氏度：");

        int mode = scanner.nextInt();

        float fd =scanner.nextFloat();

        if(mode==0){
            System.out.println(fd+"摄氏度="+fd*9/5+32+"华氏度");
        }else{
            System.out.println(fd+"华氏度="+((fd-32)*5/9)+"摄氏度");
        }

        scanner.close();
    }

    /**
     输出九九乘法表
     */
    public static void multiplicationTable() {
        for(int i=1;i<=9;i++){

            for(int j=1;j<=i;j++){
                int he=i*j;
                System.out.print(j+"*"+i+"="+he+"   ");
            }
            System.out.println();
        }
    }

    /**
     求 1 ~ n 的和
     */
    public static void sum1ToN() {
        Scanner scanner = new Scanner(System.in);
        int sum = 0;

        System.out.print("输入n：");

        int n = scanner.nextInt();

        // 使用 for 循环方式，请注释掉 while 循环方式
        for(int i=1;i<=n;i++){
            sum+=i;
        }

        // 使用 while 循环方式，请注释掉 for 循环方式
//        int i=1;
//        while(i<=n){
//            sum+=i;
//            i++;
//        }

        System.out.printf("1 ~ %d之和：%d", n, sum);

        scanner.close();
    }

    /**
     输出 1 ~ n 所有能被 3 整除的数
     */
    public static void divisibleBy3() {
        Scanner scanner = new Scanner(System.in);

        System.out.print("输入n：");

        int n = scanner.nextInt();
        int gs=0;
        for(int i=3;i<=n;i=i+3){

            System.out.println(i);
            gs++;
            if(gs%10==0){
                System.out.println();
            }
        }

        scanner.close();
    }

    /**
     输出 100 ~ 999 所有水仙花数
     */
    public static void narcissisticNumber() {
        for(int i=100;i<=999;i++){
            int zs=i/100;
            int gs=i%10;
            int ss=i/10%10;
            if(zs*zs*zs+gs*gs*gs+ss*ss*ss==i){
                System.out.println(i);
            }
        }
    }

    /**
     输出 1 ~ n 所有质数
     */
    public static void primeNumber() {
        Scanner scanner = new Scanner(System.in);

        System.out.print("输入n：");

        int n = scanner.nextInt();
        int gs=0;
        for(int i=2;i<=n;i++){
            int f=0;
            for(int j=2;j<i;j++){
                if(i%j==0){
                    f=1;
                    break;
                }
            }
            if(f==0){
                System.out.println(i);
                gs++;
                if(gs%10==0){
                    System.out.println();
                }
            }
        }

        scanner.close();
    }

    /**
     猜数字游戏
     */
    public static void numberGuess() {
        Scanner scanner = new Scanner(System.in);
        Random random = new Random();
        int r = random.nextInt(100);
        System.out.println("数字已生成");
        int shu=scanner.nextInt();
        while(shu!=r){
            if(shu>r){
                System.out.println("大了");
                shu=scanner.nextInt();
            }else{
                System.out.println("小了");
                shu=scanner.nextInt();
            }

        }
        System.out.println("中了");

        scanner.close();
    }

    /**
     输出斐波那契数列 - for 循环
     */
    public static void fibonacciFor() {
        Scanner scanner = new Scanner(System.in);

        System.out.print("输入n：");

        int n = scanner.nextInt();
        if(n==1||n==2){
            System.out.println(1);
        }else{
            int z=1;
            int q=1;
            for(int i=3;i<=n;i++){
                int t=z;
                z=z+q;
                q=t;
            }
        }

        scanner.close();
    }

    /**
     输出斐波那契数列 - 递归
     */
    public static int fibonacciRecursion(int n) {
        // 此处完成代码
        if(n==1 || n==2){
            return 1;
        }else{
            return fibonacciRecursion(n-1)+fibonacciRecursion(n-2);
        }
         // 有必要的话这句也要改掉
    }

    /**
     输出斐波那契数列 - 调用递归
     */
    public static void fibonacci() {
        Scanner scanner = new Scanner(System.in);

        System.out.print("输入n：");

        int n = scanner.nextInt();

        int r = fibonacciRecursion(n);

        System.out.println(r);

        scanner.close();
    }

    /**
     计算阶乘 - for 循环
     */
    public static void factorialFor() {
        Scanner scanner = new Scanner(System.in);

        System.out.print("输入n：");

        int n = scanner.nextInt();

        int f = 1;

        for(int i=1;i<=n;i++){
            f*=i;
        }

        System.out.println(f);

        scanner.close();
    }

    /**
     计算阶乘 - 递归
     */
    public static int factorialRecursion(int n) {
        if(n==1){
            return 1;
        }else{
            return n=n*factorialRecursion(n-1);
        }

    }

    /**
     计算阶乘 - 调用递归
     */
    public static void factorial() {
        Scanner scanner = new Scanner(System.in);

        System.out.print("输入n：");

        int n = scanner.nextInt();

        int r = factorialRecursion(n);

        System.out.println(r);

        scanner.close();
    }

    /**
     统计字符串中英文字母、数字、空格的数量
     */
    public static void stringStatistic() {
        Scanner scanner = new Scanner(System.in);

        System.out.print("输入字符串，只包含英文字母、数字、空格：");

        String s = scanner.nextLine();

        int letters = 0;
        int digits = 0;
        int spaces = 0;
        char[] cArray = s.toCharArray();
        for(int i=0;i<s.length();i++){
            if(cArray[i]==' '){
                spaces++;
            }
            if(cArray[i]>=48 && cArray[i]<=57){
                digits++;
            }
            if(( cArray[i]>=65 && cArray[i]<=65+25)||( cArray[i]>=97 && cArray[i]<=97+25)){
                letters++;
            }
        }


        System.out.printf("字母有%d个\n", letters);
        System.out.printf("数字有%d个\n", digits);
        System.out.printf("空格有%d个", spaces);

        scanner.close();
    }

    /**
     检查一个字符串是否为回文
     */
    public static void palindrome() {
        Scanner scanner = new Scanner(System.in);

        System.out.print("输入字符串：");

        String s = scanner.nextLine();
        char[] ca = s.toCharArray();
        if(s.length()%2==0){
            String aa=s.substring(0,s.length()/2-1);
            String bb=s.substring(s.length()/2);
        }else{
            String aa=s.substring(0,(s.length()-1)/2-1);
            String bb=s.substring((s.length()+1)/2);
        }


        scanner.close();
    }

    /**
     数组逆序输出
     */
    public static void reverseArray() {
        int[] numbers = randomInitIntArray(30, 0, 10);
        int[] anum=new int[30];
        for(int i=0;i<30;i++){
            anum[i]=numbers[29-i];
        }
        numbers=anum;
        printArray(numbers);
    }

    /**
     移动 0 至数组末尾
     */
    public static void move0ToEnd() {
        int[] numbers = randomInitIntArray(30, 0, 10);
        int[] anum=new int[30];
        int gs=0;
        for(int i=0;i<30;i++){
            if(numbers[i]==0){

            }else{
                anum[gs]=numbers[i];
                gs++;
            }

        }
        for(int i=gs;i<30;i++){
            anum[gs]=0;
        }
        numbers=anum;
        printArray(numbers);
    }

    /**
     数组的轮转
     */
    public static void rotateArray() {
        int[] numbers = randomInitIntArray(30, 0, 10);
        Scanner scanner = new Scanner(System.in);

        System.out.print("输入数组轮转的位置：");

        int m = scanner.nextInt();
        int[] anum=new int[30];
        int ind=0;
        for(int i=0;i<30;i++){

            if(i-m<0){
                ind=i-m+30*((m-i)/30+1);
            }else{
                ind=i-m;
            }
            anum[ind]=numbers[i];

        }
        numbers=anum;
        printArray(numbers);

        scanner.close();
    }

    /**
     最长公共前缀
     */
    public static void maxCommonPrefix() {
        Scanner scanner = new Scanner(System.in);

        System.out.print("输入第1个字符串：");
        String s1 = scanner.nextLine();
        System.out.print("输入第2个字符串：");
        String s2 = scanner.nextLine();

        // 此处完成代码
        int he=0;
        int cd=s2.length()<s1.length()?s2.length():s1.length();
        for(int i=0;i<cd;i++){
            String ss1 = s1.substring(0,i);
            String ss2 = s2.substring(0,i);
            if(ss1==ss2){
                he++;
            }else{
                break;
            }

        }
        System.out.println(he>0?he:"没有公共前缀");
        scanner.close();
    }

    /**
     合并两个有序数组
     */
    public static void mergeArray() {
        int[] array1 = randomInitIntArray(10, 0, 10);
        int[] array2 = randomInitIntArray(20, 0, 10);

        bubbleSort(array1);
        bubbleSort(array2);

        System.out.print("排序后的array1：");
        printArray(array1);
        System.out.print("排序后的array2：");
        printArray(array2);

        int[] array = new int[array1.length + array2.length];

        // 此处完成代码
        int aa1=0;
        int aa2=0;
        for(int i=0;i<array.length;i++){
            if(array1[aa1]>array2[aa2]){
                array[i]=array1[aa1];
                aa1++;
            } else if (array1[aa1]<array2[aa2]) {
                array[i]=array2[aa2];
                aa2++;
            }else{
                array[i]=array1[aa1];
                aa1++;
                aa2++;

            }
        }

        printArray(array);
    }

    /**
     二维数组对角线元素求和
     */
    public static void sumDiagonal() {
        int[][] matrix = randomInitIntMatrix(10, 10, 0, 10);

        int sum = 0;

        for(int i=0;i<10;i++){
            for(int j=0;j<10;j++){
                if(i==j || i+j==9 ){
                    sum+=matrix[i][j];
                }
            }
        }

        System.out.println("矩阵对角线之和为：" + sum);
    }

    /**
     矩阵转置（二维数组行列互换）
     */
    public static void transposition() {
        int[][] matrix1 = randomInitIntMatrix(3, 4, 0, 10);
        int[][] matrix2 = randomInitIntMatrix(4, 3, 0, 1);

        for(int i=0;i<3;i++){
            for(int j=0;j<4;j++){
                matrix2[j][i]=matrix1[i][j];
            }
        }

        System.out.println("转置后的矩阵：");
        printMatrix(matrix2);
    }

    public static void main(String[] args) {
//        leapYear();
//        grade();
//        convCF();
//       multiplicationTable();
//        sum1ToN();
//        divisibleBy3();
//        narcissisticNumber();
//        primeNumber();
//        numberGuess();
//        fibonacciFor();
//        fibonacci();
//        factorialFor();
//        factorial();
//        stringStatistic();
//        palindrome();
//        reverseArray();
//        move0ToEnd();
//        rotateArray();
//        maxCommonPrefix();
//        mergeArray();
//        sumDiagonal();
//      transposition();
    }

    /**
     * 随机生成 int 数组
     * @param length: int (数组长度)
     * @param floor: int (数组中元素值的下限)
     * @param ceiling: int (数组中元素值的上限)
     * @return : int[] (初始化完成的数组)
     */
    public static int[] randomInitIntArray(int length, int floor, int ceiling) {
        Random random = new Random();
        int[] array = new int[length];

        for (int i = 0; i < length; i++) {
            array[i] = random.nextInt(ceiling - floor) + floor;
        }

        System.out.print("随机int数组：");
        printArray(array);

        return array;
    }

    /**
     * 随机生成2维 int 数组
     * @param m: int (2维数组的行数)
     * @param n: int (2维数组的列数)
     * @param floor: int (数组中元素值的下限)
     * @param ceiling: int (数组中元素值的上限)
     * @return : int[][] (初始化完成的2维数组)
     */
    public static int[][] randomInitIntMatrix(int m, int n, int floor, int ceiling) {
        Random random = new Random();
        int[][] matrix = new int[m][n];

        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                matrix[i][j] = random.nextInt(ceiling - floor) + floor;
            }
        }

        System.out.println("随机2维int数组：");
        printMatrix(matrix);

        return matrix;
    }

    /**
     * 打印 int 数组
     * @param array: int[] (要打印的int数组)
     */
    public static void printArray(int[] array) {
        for (int e : array) {
            System.out.print(e + " ");
        }
        System.out.println();
    }

    /**
     * 打印2维 int 数组
     * @param matrix: int[][] (要打印的2维int数组)
     */
    public static void printMatrix(int[][] matrix) {
        for (int[] array : matrix) {
            printArray(array);
        }
    }


    /**
     * 冒泡排序
     * @param array: int[] (要排序的int数组)
     */
    public static void bubbleSort(int[] array) {
        int n = array.length;
        boolean swapped;
        for (int i = 0; i < n - 1; i++) {
            swapped = false;
            for (int j = 0; j < n - i - 1; j++) {
                if (array[j] > array[j + 1]) {
                    // 交换 array[j+1] 和 array[j]
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                    swapped = true;
                }
            }
            // 如果没有元素交换，说明数组已经排序好
            if (!swapped) break;
        }
    }
}